<template>
  <div id="app">
    <keep-alive>
      <router-view v-if="$route.meta.keepAlive"></router-view>
    </keep-alive>
    <router-view v-if="!$route.meta.keepAlive"></router-view>
  </div>
</template>

<script>
import hilinkCommon from "./hilink/common.js";
import storeManage from "../../src/pages/store/index";
import { mapState, mapMutations, mapActions } from "vuex";
export default {
  name: "App",
  data() {
    return {
      setMasterVolumeVal: 0,
    };
  },
  created() {
    //获取状态栏高度
    hilinkCommon.getStatusBarHeight();
    //获取是否暗黑模式
    hilinkCommon.getDeviceMode();
    document
      .querySelector(":root")
      .setAttribute("data-user-color-scheme", "light");
  },
  computed: {
    ...mapState({
      charactValueChangeRes: (state) => state.ble.charactValueChangeRes,
      // charactValueChangeRes: (state) => state.ble.charactValueChangeRes
    }),
    // ...mapGetters({ getDeviceInfo: "ble/getDeviceInfo",getMasterVolume:'ble/getMasterVolume' }),
  },
  methods: {
    ...mapActions("ble", ["setMasterVolume"]),
    //  this.$nextTick(() => {
    //         storeManage.commit("ble/setMasterVolume", this.setMasterVolumeVal); //关闭
    //       })
  },

  watch: {
    "charactValueChangeRes.data": {
      deep: true,
      // immediate: true,
      handler(newV, odlNew) {
        //处理设备自主上报
        if (newV.length == 584) {
          // console.log("自主上报数据", newV);
          let len = newV.length;
          let arr = [];
          for (let i = 12; i < len; i += 2) {
            let sli = newV.slice(i, i + 2);
            arr.push(sli);
          }
          console.log("处理设备自主上报------------", arr);
          //总音量
          console.log("自主上报总音量----", parseInt(arr[0], 16));
          this.setMasterVolumeVal = parseInt(arr[0], 16);
          // this.setMasterVolume(parseInt(arr[0], 16))
          storeManage.dispatch(
            "ble/setMasterVolumeAction",
            this.setMasterVolumeVal
          );
          //音源选择
          switch (arr[1]) {
            case "00":
              storeManage.commit("ble/setSourceSelection", 0);
              break;
            case "01":
              storeManage.commit("ble/setSourceSelection", 1);
              break;
            case "02":
              storeManage.commit("ble/setSourceSelection", 2);
              break;
            case "03":
              storeManage.commit("ble/setSourceSelection", 3);
              break;
          }
          //混音设置  原 车dsd
          // let setOriginalCarAudioval = parseInt(arr[2], 16)
          storeManage.commit("ble/setOriginalCarAudio", parseInt(arr[2], 16));
          storeManage.commit("ble/setHicarCarAudio", parseInt(arr[3], 16));
          storeManage.commit("ble/setAUXCarAudio", parseInt(arr[4], 16));
          storeManage.commit("ble/setUSBCarAudio", parseInt(arr[5], 16));

          //降噪
          storeManage.commit("ble/setValDenoise", parseInt(arr[6], 16));

          //声场选择
          switch (arr[7]) {
            case "00":
              storeManage.commit("ble/setSoundField", 0);
              break;
            case "01":
              storeManage.commit("ble/setSoundField", 1);
              break;
            case "02":
              storeManage.commit("ble/setSoundField", 2);
              break;
            case "03":
              storeManage.commit("ble/setSoundField", 3);
              break;
          }

          //声场选择 前后左右
          storeManage.commit("ble/setsoundFieldback", parseInt(arr[8], 16));
          storeManage.commit(
            "ble/stesoundFieldLeftRight",
            parseInt(arr[9], 16)
          );

          //音效选择：音效编号
          switch (arr[10]) {
            case "00":
              storeManage.commit("ble/setSoundChoice", 0);
              break;
            case "01":
              storeManage.commit("ble/setSoundChoice", 1);
              break;
            case "02":
              storeManage.commit("ble/setSoundChoice", 2);
              break;
            case "03":
              storeManage.commit("ble/setSoundChoice", 3);
              break;
            case "04":
              storeManage.commit("ble/setSoundChoice", 4);
              break;
            case "05":
              storeManage.commit("ble/setSoundChoice", 5);
              break;
            case "06":
              storeManage.commit("ble/setSoundChoice", 6);
              break;
            case "07":
              storeManage.commit("ble/setSoundChoice", 7);
              break;
            case "08":
              storeManage.commit("ble/setSoundChoice", 8);
              break;
            case "09":
              storeManage.commit("ble/setSoundChoice", 9);
              break;
            case "0A":
              storeManage.commit("ble/setSoundChoice", 10);
              break;
            case "0B":
              storeManage.commit("ble/setSoundChoice", 11);
              break;
            case "0C":
              storeManage.commit("ble/setSoundChoice", 12);
              break;
            case "0D":
              storeManage.commit("ble/setSoundChoice", 13);
              break;
            case "0E":
              storeManage.commit("ble/setSoundChoice", 14);
              break;
            case "0F":
              storeManage.commit("ble/setSoundChoice", 15);
              break;
            case "10":
              storeManage.commit("ble/setSoundChoice", 16);
              break;
            case "11":
              storeManage.commit("ble/setSoundChoice", 17);
              break;
            case "12":
              storeManage.commit("ble/setSoundChoice", 18);
              break;
            case "13":
              storeManage.commit("ble/setSoundChoice", 19);
              break;
            case "14":
              storeManage.commit("ble/setSoundChoice", 20);
              break;
          }
          //喇叭设置
          storeManage.commit("ble/setData", {
            hornDelay: parseInt(arr[11] + arr[12], 16),
          });
          console.log(
            "====下标11-",
            arr[11],
            "下标12-",
            arr[12],
            "----下标11+下标12",
            // parseInt(arr[11], 16) + parseInt(arr[12], 16)
            parseInt(arr[11] + arr[12], 16)
          );
          // //喇叭音量
          // console.log("===========================", arr[19]);
          storeManage.commit("ble/setData", {
            SpeakerVolume: parseInt(arr[19], 16),
          });

          // switch (arr[20]) {
          //   case "00":
          //     storeManage.commit("ble/setData", {
          //       SpeakerMuteReverse: true,
          //     });
          // }
           console.log("=================相位上报自主", arr[20]);
          storeManage.commit("ble/setData", {
            SpeakerNumber: arr[20],
          });
          //车载/家用模式
          // console.log("===========================", arr[27]);
          switch (arr[27]) {
            case "01":
              storeManage.commit("ble/setModeSelection", 0);
              break;
            case "02":
              storeManage.commit("ble/setModeSelection", 1);
              break;
          }
        }

        //处理手动上报
        console.log("监听设备手动上报数据", newV);
        // 开关
        if (newV.includes("F0AA04000E1001")) {
          console.log("开关--------------------------", newV);
          let len = newV.length;
          let arr = [];
          for (let i = 12; i < len; i += 2) {
            let sli = newV.slice(i, i + 2);
            arr.push(sli);
          }

          if (arr[1] == "00") {
            storeManage.commit("ble/setBleSatuts", 4); //关闭
            storeManage.commit("ble/setModeSelection", null);
          } else if (arr[1] == "01") {
            storeManage.commit("ble/setBleSatuts", 2); //开启
            console.log("开关数组--------------------------", arr);
            if (arr[0] == "01") {
              storeManage.commit("ble/setModeSelection", 0);
            } else if (arr[0] == "02") {
              storeManage.commit("ble/setModeSelection", 1);
            }
          }

          // newV;
          // console.log("开关+++++++++++++++++", newV.slice(14, 16));
          // let str = newV.slice(14, 16);
          // if (str == "00") {
          //   storeManage.commit("ble/setBleSatuts", 4);
          // } else if (str == "01") {
          //   storeManage.commit("ble/setBleSatuts", 2);

          // }
        }
        // // 模式选择
        // if (newV.includes("F0AA04000E10")) {
        //   let str = newV.slice(12, 14);
        //   console.log("模式选择+++++++++++++++++", newV.slice(12, 14));
        //   if (str == "01") {
        //     storeManage.commit("ble/setModeSelection", 0);
        //   } else if (str == "02") {
        //     // console.log("+++++++++++++++++", newV.slice(12, 15));
        //     storeManage.commit("ble/setModeSelection", 1);
        //   }
        // }
        //音源选择
        if (newV.includes("F0AA03000DF5")) {
          let str = newV.slice(12, 14);
          console.log("音源选择+++++++++++++++++", newV.slice(12, 14));
          switch (str) {
            case "00":
              storeManage.commit("ble/setSourceSelection", 0);
              break;
            case "01":
              storeManage.commit("ble/setSourceSelection", 1);
              break;
            case "02":
              storeManage.commit("ble/setSourceSelection", 2);
              break;
            case "03":
              storeManage.commit("ble/setSourceSelection", 3);
              break;
          }
        }
        //音效选择
        if (newV.includes("F0AA03000D0D")) {
          let str = newV.slice(12, 14);
          console.log("音效选择+++++++++++++++++", newV.slice(12, 14));
          switch (str) {
            case "00":
              storeManage.commit("ble/setSoundChoice", 0);
              break;
            case "01":
              storeManage.commit("ble/setSoundChoice", 1);
              break;
            case "02":
              storeManage.commit("ble/setSoundChoice", 2);
              break;
            case "03":
              storeManage.commit("ble/setSoundChoice", 3);
              break;
            case "04":
              storeManage.commit("ble/setSoundChoice", 4);
              break;
            case "05":
              storeManage.commit("ble/setSoundChoice", 5);
              break;
            case "06":
              storeManage.commit("ble/setSoundChoice", 6);
              break;
            case "07":
              storeManage.commit("ble/setSoundChoice", 7);
              break;
            case "08":
              storeManage.commit("ble/setSoundChoice", 8);
              break;
            case "09":
              storeManage.commit("ble/setSoundChoice", 9);
              break;
            case "0A":
              storeManage.commit("ble/setSoundChoice", 10);
              break;
            case "0B":
              storeManage.commit("ble/setSoundChoice", 11);
              break;
            case "0C":
              storeManage.commit("ble/setSoundChoice", 12);
              break;
            case "0D":
              storeManage.commit("ble/setSoundChoice", 13);
              break;
            case "0E":
              storeManage.commit("ble/setSoundChoice", 14);
              break;
            case "0F":
              storeManage.commit("ble/setSoundChoice", 15);
              break;
            case "10":
              storeManage.commit("ble/setSoundChoice", 16);
              break;
            case "11":
              storeManage.commit("ble/setSoundChoice", 17);
              break;
            case "12":
              storeManage.commit("ble/setSoundChoice", 18);
              break;
            case "13":
              storeManage.commit("ble/setSoundChoice", 19);
              break;
            case "14":
              storeManage.commit("ble/setSoundChoice", 20);
              break;
          }
        }
        //音场选择
        if (newV.includes("F0AA05000DF2")) {
          let str = newV.slice(12, 14);
          console.log("音场选择+++++++++++++++++", newV.slice(12, 14));
          switch (str) {
            case "00":
              storeManage.commit("ble/setSoundField", 0);
              break;
            case "01":
              storeManage.commit("ble/setSoundField", 1);
              break;
            case "02":
              storeManage.commit("ble/setSoundField", 2);
              break;
            case "03":
              storeManage.commit("ble/setSoundField", 3);
              break;
          }
          console.log(
            "声场调节前后--------------------------",
            newV.slice(14, 16),
            "转换后" + parseInt(newV.slice(14, 16), 16)
          );
          let frontAndBack = newV.slice(14, 16);
          storeManage.commit("ble/setData", {
            soundFieldback: parseInt(frontAndBack, 16),
          });
          // storeManage.commit("ble/setsoundFieldback", parseInt(frontAndBack, 16));
          console.log(
            "声场调节左右--------------------------",
            newV.slice(16, 18),
            "转换后" + parseInt(newV.slice(16, 18), 16)
          );
          let leftrigth = newV.slice(16, 18);
          storeManage.commit("ble/setData", {
            soundFieldLeftRight: parseInt(leftrigth, 16),
          });
          //  storeManage.commit("ble/stesoundFieldLeftRight", parseInt(leftrigth, 16));
        }
        // 总音量
        if (newV.includes("F0AA03000DF4")) {
          let str = newV.slice(12, 14);
          console.log(
            "总音量+++++++++++++++++",
            newV.slice(12, 14),
            "----------------",
            parseInt(str, 16)
          );
          let data = parseInt(str, 16);
          storeManage.commit("ble/setMasterVolume", data);
        }
        //降噪
        if (newV.includes("F0AA03000DF7")) {
          let str = newV.slice(12, 14);
          console.log(
            "降噪+++++++++++++++++",
            newV.slice(12, 14),
            "----------------",
            parseInt(str, 16)
          );
          let data = parseInt(str, 16);
          storeManage.commit("ble/setValDenoise", data);
        }
        //混音设置 原车音源
        if (newV.includes("F0AA06000DF1")) {
          console.log("开关--------------------------", newV);
          let len = newV.length;
          let arr = [];
          for (let i = 12; i < len; i += 2) {
            let sli = newV.slice(i, i + 2);
            arr.push(parseInt(sli, 16));
            // arr.push(sli)
          }
          console.log("原车音源--------------------------", arr);
          storeManage.commit("ble/setOriginalCarAudio", arr[0]);
          storeManage.commit("ble/setHicarCarAudio", arr[1]);
          storeManage.commit("ble/setAUXCarAudio", arr[2]);
          storeManage.commit("ble/setUSBCarAudio", arr[3]);
          // let str = newV.slice(12, 14);
          // console.log(
          //   "混音设置原车音源+++++++++++++++++",
          //   newV.slice(12, 14),
          //   "----------------",
          //   parseInt(str, 16)
          // );
          // let data = parseInt(str, 16);
          // storeManage.commit("ble/setOriginalCarAudio", data);
        }
        //声场调节
        // if (newV.includes("F0AA05000DF2")) {
        //   console.log("声场调节--------------------------", newV);
        // }
        //喇叭设置
        if (newV.includes("F0AA05000D09")) {
          console.log("喇叭设置--------------------------", newV);
          // F05505000D09000064
          let len = newV.length;
          let arr = [];
          for (let i = 12; i < len; i += 2) {
            let sli = newV.slice(i, i + 2);
            arr.push(sli);
            // arr.push(sli)
          }
          console.log("喇叭设置---上报-------", arr);

          storeManage.commit("ble/setData", {
            loudspeaker: arr[0],
          });
          //  arr第一位代表喇叭位置
          switch (arr[0]) {
            case "00":
              storeManage.commit("ble/setData", {
                ic_hornActive: true,
              });
              break;
            case "01":
              storeManage.commit("ble/setData", {
                ic_beforeActive: true,
              });
              break;
            case "02":
              storeManage.commit("ble/setData", {
                ic_hornActiveTow: true,
              });
              break;
            case "03":
              storeManage.commit("ble/setData", {
                ic_beforeActiveTow: true,
              });
              break;
          }
          console.log("喇叭设置---相位上报-------", arr[1]);
          storeManage.commit("ble/setData", {
            SpeakerNumber: arr[1],
          });
          switch (arr[1]) {
            case "00":
              storeManage.commit("ble/setData", {
                SpeakerMuteReverse: true,
              });
              break;
            case "01":
              storeManage.commit("ble/setData", {
                SpeakerMuteReverse: false,
              });
              break;
            case "10":
              storeManage.commit("ble/setData", {
                SpeakerMute: true,
              });
              break;
            case "11":
              storeManage.commit("ble/setData", {
                SpeakerMute: false,
              });
              break;
          }
          //喇叭音量
          storeManage.commit("ble/setData", {
            SpeakerVolume: parseInt(arr[2], 16),
          });
        }
        //喇叭设置 延迟
        if (newV.includes("F0AA05000D0B")) {
          console.log("喇叭延迟--------------------------", newV);
          let str = newV.slice(14, 18);
          console.log(
            "喇叭设置 延迟+++++++++++++++++",
            newV.slice(14, 18),
            "----------------",
            parseInt(str, 16)
          );
          let data = parseInt(str, 16);
          storeManage.commit("ble/setData", { hornDelay: data });
        }
        // if (newV.includes("F05505000D09")) {
        //   console.log("喇叭-音量--------------------------", newV);
        //   console.log(
        //     "喇叭设置 音量+++++++++++++++++",
        //     newV.slice(12),
        //     "----------------",
        //     parseInt(newV.slice(12), 16)
        //   );
        // }
      },
    },
  },
  methods: {},
};
</script>
<style lang="less">
#app {
  background: var(--bg);
}
</style>
